//
//  SEUtils.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit
import Toast_Swift

// MARK: print
func SELog<T>(_ message: T, file: String = #file, function: String = #function, lineNumber: Int = #line) {
    #if DEBUG
        let fileName = (file as NSString).lastPathComponent
        print("[\(fileName):funciton:\(function):line:\(lineNumber)]- \(message)")
    #endif
}

func dispatch_async_safely_main_queue(block: @escaping () -> Void) {
    dispatch_async_safely_queue(DispatchQueue.main, block: block)
}

func dispatch_async_safely_queue(_ queue: DispatchQueue, block: @escaping () -> Void) {
    if queue === DispatchQueue.main && Thread.isMainThread {
        block()
    } else {
        queue.async { block() }
    }
}

func presentImageBrowser(imageView: UIImageView?, originalUrl: String?) {
    let imageBrowser = SEImageBrowser(imageView: imageView, originalUrl: originalUrl)
    imageBrowser.modalPresentationStyle = .custom
    imageBrowser.modalTransitionStyle = .crossDissolve
    UIViewController.current?.present(imageBrowser, animated: true, completion: nil)
}

func SEOpenSettingURL() {
    let url = URL(string: UIApplication.openSettingsURLString)!
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

let mainWindow = UIApplication.shared.keyWindow
private let toastDuration: TimeInterval = 2.0

struct SEAlert {
    static func showImagePickerSheet(in target: UIViewController?, cameraHandler: ((UIAlertAction) -> Void)?, photoHandler: ((UIAlertAction) -> Void)?) {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        let cancel = UIAlertAction(title:"取消", style: .cancel, handler: nil)
        let cameraAction = UIAlertAction(title: "拍照", style: .default, handler: cameraHandler)
        let photoAction = UIAlertAction(title:"从手机相册选择", style: .default, handler: photoHandler)
        
        alert.addAction(cancel)
        alert.addAction(cameraAction)
        alert.addAction(photoAction)
        target?.present(alert, animated:true, completion:nil)
    }
    
    static func show(in target: UIViewController?, title: String?, message: String? = nil, confirmTitle: String = "确认", confirmHandler: ((UIAlertAction) -> Void)?) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

        let cancel = UIAlertAction(title:"取消", style: .destructive, handler: nil)
        let confirm = UIAlertAction(title: confirmTitle, style: .default, handler: confirmHandler)
        
        alert.addAction(cancel)
        alert.addAction(confirm)
        target?.present(alert, animated:true, completion:nil)
    }

    static func showUpdateAlert(title: String?, message: String?, updateUrl: String, target: UIViewController?) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "现在更新", style: .default, handler: { (_) in
            if #available(iOS 10, *) {
                UIApplication.shared.open(URL(string: updateUrl)!, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(URL(string: updateUrl)!)
            }
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        target?.present(alert, animated: true, completion: nil)
    }
}

struct SEHUD {
    static func show(in view: UIView? = nil, message: String?, position: ToastPosition = .bottom) {
        dispatch_async_safely_main_queue {
            let superview = view == nil ? mainWindow : view
            let style = setToastStyle(.black)
            superview?.hideToast()
            superview?.makeToast(message, duration: toastDuration, position: position, style: style)
        }
    }
    
    static func showError(in view: UIView? = nil, message: String?, position: ToastPosition = .bottom) {
        dispatch_async_safely_main_queue {
            let superview = view == nil ? mainWindow : view
            let style = setToastStyle(Asset.warn.color)
            superview?.hideToast()
            superview?.makeToast(message, duration: toastDuration, position: position, style: style)
        }
    }
    
    private static func setToastStyle(_ backgroundColor: UIColor) -> ToastStyle {
        var style = ToastStyle()
        style.messageFont = UIFont.medium(16.0)
        style.backgroundColor = backgroundColor.withAlphaComponent(0.75)
        style.messageColor = .white
        return style
    }
    
    static func showActivity(in view: UIView? = nil) {
        dispatch_async_safely_main_queue {
            let superview = view == nil ? mainWindow : view
            ToastManager.shared.style.activityBackgroundColor = UIColor.black.withAlphaComponent(0.36)
            superview?.makeToastActivity(.center)
        }
    }
    
    static func hideActivity(in view: UIView? = nil) {
        dispatch_async_safely_main_queue {
            let superview = view == nil ? mainWindow : view
            superview?.hideToastActivity()
        }
    }
}
